home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / PowerPlant / CPreferenceMgr / CPreferenceMgr.cp next >
Encoding:
Text File  |  1997-06-30  |  2.7 KB  |  116 lines  |  [TEXT/CWIE]

  1. /*******************************************************************************\
  2.  CPreferenceMgr - a class for managing preferences
  3.  Dan Crevier 6/19/97 <mailto:Dan.Crevier@pobox.com>
  4.  
  5.  To add preferences to your application, make a subclass of CPreferenceMgr with
  6.  member values for your prefs, and implement:
  7.  
  8.  Read(LFileStream &prefFile) - to read preferences from a stream, like:
  9.        prefFile >> data1 >> data2
  10.      Throw execptions if there is a problem with the data, and it will be
  11.      flagged corrupt, and the default values will be used
  12.  Write(LFileStream &prefFile) - to write the preferences to a stream, like:
  13.        prefFile << data1 << data2
  14.  ReportError(const LStr255 &errorString) - to report errors - like corrupt
  15.      preferences
  16.  
  17.  In your application, add a member variable for your preferences.  In the
  18.  constructor, add:
  19.  
  20.     mPreferences = new CMyPrefs;
  21.     mPreferences->Init("\pMy Pref file name", kMyCreator);
  22.  
  23.  In your destructor, add:
  24.  
  25.      mPreferences->FlushPreferences();
  26.     delete mPreferences;
  27.  
  28.  And you can also flush the preferences at any other time, like when the
  29.  user closes a preference dialog or something.
  30.  
  31.  Thanks to John C. Daub for lots of useful suggestions
  32.  
  33. \*******************************************************************************/
  34.  
  35. #include "CPreferenceMgr.h"
  36.  
  37. #include <LFileStream.h>
  38. #include <LString.h>
  39.  
  40. #include <Folders.h>
  41.  
  42. CPreferenceMgr::CPreferenceMgr()
  43.     : mInited(false)
  44. {
  45. }
  46.  
  47. void CPreferenceMgr::Init(const LStr255 &prefName, OSType creatorSpec,
  48.         OSType fileType)
  49. {
  50.     OSErr err;
  51.  
  52.     // Find the preferences folder
  53.     FailOSErr_(FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  54.         &mPrefSpec.vRefNum, &mPrefSpec.parID));
  55.         
  56.     // Make an FSSpec to the preference file
  57.     err = ::FSMakeFSSpec(mPrefSpec.vRefNum, mPrefSpec.parID, prefName, &mPrefSpec);
  58.  
  59.     LFileStream prefFile(mPrefSpec);
  60.     
  61.     mInited = true;
  62.  
  63.     if (err == fnfErr) // file does not exist yet
  64.     {
  65.         prefFile.CreateNewDataFile(creatorSpec, fileType);
  66.         SetDefaultPreferences();
  67.         FlushPreferences();
  68.     }
  69.     else if (err == noErr)
  70.     {
  71.         try
  72.         {
  73.             prefFile.OpenDataFork(fsRdPerm);
  74.             Read(prefFile);
  75.         }
  76.         catch(...)
  77.         {
  78.             ReportError("\pPreferences were corrupt. Default values used.");
  79.             SetDefaultPreferences();
  80.         }
  81.     }
  82.     else
  83.     {
  84.         FailOSErr_(err);
  85.     }
  86. }
  87.  
  88. CPreferenceMgr::~CPreferenceMgr()
  89. {
  90. }
  91.  
  92. // write the preferences to disk
  93. void CPreferenceMgr::FlushPreferences()
  94. {
  95.     Assert_(mInited);
  96.     try
  97.     {
  98.         LFileStream prefFile(mPrefSpec);
  99.         
  100.         prefFile.OpenDataFork(fsRdWrPerm);
  101.         prefFile.SetLength(0);
  102.         
  103.         Write(prefFile);
  104.     }
  105.     catch(...)
  106.     {
  107.         ReportError("\pThere was an error writing the preferences to disk.");
  108.     }
  109. }
  110.  
  111. // override this to use whatever mechanism you use to report errors
  112. void CPreferenceMgr::ReportError(const LStr255 &/*errorString*/)
  113. {
  114.     // ::DebugStr(errorString);
  115. }
  116.